/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 *
 */

import { expect, test } from '@jest/globals';

import { ModuleBuildProfile } from '../../../src/options/build/module-build-profile.js';
import { OhosLogger } from '../../../src/utils/log/ohos-logger.js';
import { ValidateUtil } from '../../../src/utils/validate/validate-util.js';

test('test duplicateName', () => {
  expect(ValidateUtil.searchDuplicateName([{name: 'default'}, {name: 'default'}])).toStrictEqual(['default']);
  expect(ValidateUtil.searchDuplicateName([{name: 'default'}, {name: 'default2'}])).toStrictEqual([]);

  const products: any[] = [{
    name: 'default',
    signingConfig: 'default'
  }, {
    name: 'default',
    signingConfig: 'default2'
  }];
  expect(ValidateUtil.searchDuplicateName(products)).toStrictEqual(['default']);

  const targets: ModuleBuildProfile.ModuleTargetBuildOpt[] = [{name: 'default',}, {name: 'ohosTest',},
    {name: 'default',}, {name: 'ohosTest'}];
  expect(ValidateUtil.searchDuplicateName(targets)).toStrictEqual(['default', 'ohosTest']);
});

test('test defualt prodcut', () => {

  jest.spyOn(OhosLogger.prototype, 'errorMessageExit').mockImplementation(
    (message: string, ...args: unknown[]) => {
      throw new Error(message + args);
    }
  );

  const products = [{name: 'default1'}];
  expect(() => ValidateUtil.validateContainsDefault(products, 'file')).toThrowError();
  products.push({name: 'default'});
  expect(() => ValidateUtil.validateContainsDefault(products, 'file')).not.toThrow();
});
